<template>
  <div class="wrap"
       v-loading="isLoading"
       :element-loading-text="loadingText"
  >
    <StringersParams v-if="curTabName === 'param'" ref="param"></StringersParams>
    <FaceElementSelect v-if="curTabName === 'surface'" ref="surface"></FaceElementSelect>

    <div style="margin-top: 10px" class="btn">
      <el-button type="primary" v-if="curTabName === 'surface'" @click="gotoParam">
        {{ $t("message.TrussDesign.lastStep") }}
      </el-button>
      <el-button type="primary" v-if="curTabName === 'param'" @click="gotoSurface">
        {{ $t("message.TrussDesign.nextStep") }}
      </el-button>
      <el-button type="primary" v-if="curTabName === 'surface'" @click="createTruss">
        {{ $t("message.TrussDesign.generate") }}
      </el-button>
      <el-button @click="close">
        {{ $t("message.TrussDesign.cancel") }}
      </el-button>
    </div>
  </div>

</template>
<script>
import DialogMixin from "../DialogMixin";
import StringersParams from "./pages/StringersParams.vue";
import FaceElementSelect from "./pages/FaceElementSelect.vue";
import trussDesignCommand from "./TrussDesignCommand";

export default {
  name: "LongTrussDesign",
  mixins: [DialogMixin],
  components: {
    FaceElementSelect,
    StringersParams,
  },
  data() {
    return {
      curTabName: "param",
      isLoading: false,
      loadingText: this.$t("message.TrussDesign.loadingText")
    }
  },
  mounted() {
    trussDesignCommand.init(500, 500, 800, () => {
      trussDesignCommand.handlePickElement();
    })
    // 获取名称信息
    trussDesignCommand.names = {
      feature: {
        lineConvert: this.$t("message.TrussDesign.feature.lineConvert"),
      },
      stringerName: {
        typeT: this.$t("message.TrussDesign.trussTypes.typeT.name"),
        typeG: this.$t("message.TrussDesign.trussTypes.typeG.name"),
        typeZ: this.$t("message.TrussDesign.trussTypes.typeZ.name"),
        typeC: this.$t("message.TrussDesign.trussTypes.typeC.name"),
        typeVL: this.$t("message.TrussDesign.trussTypes.typeVL.name"),
        typeSL: this.$t("message.TrussDesign.trussTypes.typeSL.name"),
        typeLL: this.$t("message.TrussDesign.trussTypes.typeLL.name"),
        typeJ: this.$t("message.TrussDesign.trussTypes.typeJ.name"),
      }
    }
  },
  methods: {
    /**
     * 控制页面展示的高宽
     */
    changePluginContainerHeight() {
      trussDesignCommand.changePluginContainerHeight(
          {minWidth: 200, maxHeight: this.$el.clientHeight + 10, width: 285});
    },
    /**
     * 跳转到界面元素选择页
     */
    gotoSurface() {
      trussDesignCommand.sizeParam = this.$refs.param.params;
      trussDesignCommand.type = this.$refs.param.params.Selected;
      // 参数有效性判断
      let error = trussDesignCommand.validateTrussParams();
      if (error) {
        trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.errors.paramsErrors." + error), "warning");
        return
      }
      this.curTabName = "surface";
    },
    /**
     * 返回上一页
     */
    gotoParam() {
      this.curTabName = "param";
      trussDesignCommand.from = 1;
      this.$refs.surface.params.path.allElementIds?.forEach(j => {
        trussDesignCommand.toolQuery.clearEntityOutline(j)
      })
    },
    /**
     * 生成长桁
     */
    async createTruss() {
      this.isLoading = true;
      trussDesignCommand.selectedParam = this.$refs.surface.params;
      // 元素选择有效性判断
      let error = trussDesignCommand.validateElementsSelect();
      if (error) {
        trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.elementSelect." + error + ".message"), "warning");
        this.isLoading = false;
        return
      }
      // 创建长桁
      let ret = await trussDesignCommand.createLongTruss();
      // 处理错误
      if (ret?.errorMessage) {
        trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.errors.serviceErrors."
            + (ret.errorMessage?.message ? "message" : ret.errorMessage)), "warning");
        this.isLoading = false;
        return;
      }
      // 成功提示
      trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.success"), "success");
      this.isLoading = false;
      // 关闭界面
      this.close();
    },
    /**
     * 关闭窗口
     */
    close() {
      trussDesignCommand.closeActiveCmd();
    },
    /**
     * 临时方法
     */
    test() {
      trussDesignCommand.selectedParam = this.$refs.surface.params;
      trussDesignCommand.testFace();
    },
  }
}
</script>
<style scoped>
.wrap {
  font-size: 12px;
  padding-left: 5px;
}
</style>